package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.third.JuheAreaMapper;
import com.zjdiepu.www.ibiz.third.IJuheAreaService;
import com.zjdiepu.www.model.third.dto.JuheArea;
import com.zjdiepu.www.model.third.ext.JuheAreaExt;

/**
 * 市区Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年07月18日 18时03分<br/>
 * @since 1.0<br/>
 */
@Service
public class JuheAreaServiceImpl extends BaseService implements IJuheAreaService {
	public static final Logger logger = LoggerFactory.getLogger(JuheAreaServiceImpl.class);

	@Autowired
	private JuheAreaMapper juheAreaMapper;
	
	@Override
    public String getNameById(String areaId) {
        if(StringUtils.isNotBlank(areaId)) {
            JuheAreaExt juheAreaExt = new JuheAreaExt();
            juheAreaExt.setAreaId(areaId);
            juheAreaExt.setRows(1);
            List<JuheArea> juheAreas = juheAreaMapper.selectByExt(juheAreaExt);
            if(CollectionUtils.isNotEmpty(juheAreas)) {
            	JuheArea juheArea = juheAreas.get(0);
                if(null != juheArea){
                    return juheArea.getAreaName();
                }
            }
        }
        return null;
    }
	
	@Override
	public void saveUpdateAndDelete(JuheArea juheArea, OperateType operType) {
		switch (operType) {
		case add:
			juheArea.initInsertData(juheArea.getModifier(), juheArea.getModifierName());
			juheAreaMapper.insert(juheArea);
			break;
		case edit:
			juheArea.initUpdateData(juheArea.getModifier(), juheArea.getModifierName());
			// 影响的记录数
			int updateCount = juheAreaMapper.updateByPrimaryKeySelective(juheArea);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			juheArea.initDeleteData(juheArea.getModifier(), juheArea.getModifierName());
			// 影响的记录数
			int deleteCount = juheAreaMapper.updateByPrimaryKeySelective(juheArea);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<JuheArea> selectByExt(JuheAreaExt juheAreaExt) {
		return juheAreaMapper.selectByExt(juheAreaExt);
	}
	
	@Override
	public int countByExt(JuheAreaExt juheAreaExt){
		return juheAreaMapper.countByExt(juheAreaExt);
	}
	
	@Override
	public JuheArea selectById(String id) {
		return juheAreaMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			juheAreaMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(JuheArea juheArea) {
		return juheAreaMapper.countRefined(juheArea);
	}

	@Override
	public JuheArea selectRefined(JuheArea juheArea) {
		return juheAreaMapper.selectRefined(juheArea);
	}
	
	@Override
	public int deletedRefined(JuheArea juheArea) {
		return juheAreaMapper.deletedRefined(juheArea);
	}

	@Override
	public boolean repeat(JuheArea juheArea) {
		int count = countRefined(juheArea);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(juheArea.getId())) {
			return true;
		}
		JuheArea dbItem = selectRefined(juheArea);
		if(null == dbItem) {
			return false;
		}
		return !juheArea.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return juheAreaMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<JuheArea> juheAreas) {
		return juheAreaMapper.insertBatch(juheAreas);
	}
}
